"""
一般 Python 用于连接 MySQL 的工具：pymysql
"""
import pymysql.cursors

connection = pymysql.connect(host='localhost',
                             user='root',
                             password='123456',
                             db='test',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)
influce_rows = 0

# 保存评论
def insert_comments(music_name, comment):
    with connection.cursor() as cursor:
        sql = "INSERT INTO `comments` (`MUSIC_NAME`, `COMMENT`) VALUES (%s, %s)"
        cursor.execute(sql, (music_name, comment))
        cursor.fetchall()
        influce_rows = cursor.rowcount
    connection.commit()
    return influce_rows


# 保存音乐
def insert_music(music_id, music_name):
    with connection.cursor() as cursor:
        sql = "INSERT INTO `music` (`MUSIC_ID`, `MUSIC_NAME`) VALUES (%s, %s)"
        cursor.execute(sql, (music_id, music_name))
        influce_rows = cursor.rowcount
    connection.commit()
    return influce_rows

def dis_connect():
    connection.close()

